import 'dart:math';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter_elf/config/component_index.dart';
class HuaWeiWeatherPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HuaWeiWeatherPageState();
  }
}

class HuaWeiWeatherPageState extends State with TickerProviderStateMixin{
  double percentage = 0.0;

  double progress = 0;
  AnimationController _animationController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _animationController =
    AnimationController(vsync: this, duration: Duration(seconds: 6))
      ..addListener(() {
        setState(() {
          progress = _animationController.value * 360;
        });
      })
      ..addStatusListener((AnimationStatus status) {
        ///动画结束后启动动画
        if (status == AnimationStatus.completed) {
          _animationController.reset();
          _animationController.forward();
        }
      });

    _animationController.forward();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Container(
        color: Colors.blue,
        child: Column(
          children: <Widget>[
            Text('====='),
//        Container(
//            color: Color(0xff6681a9),
//            child: AnimatedBuilder(
//                animation: animationController,
//                builder: (context,child){
//                  return BackGrid(percentage);
//                },
////            child: BackGrid(percentage)
//            )),
            _FanWidget(
              width: 100,
              height: 100,
              progress: progress,
              color: Colors.red,
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _animationController?.dispose();
    super.dispose();
  }

}

class HuaWeiWeatherPage2 extends BaseWidget{
  @override
  BaseWidgetState<BaseWidget> getState() {
    // TODO: implement getState
    return HuaWeiWeatherPageState2();
  }
}

class HuaWeiWeatherPageState2 extends BaseWidgetState<HuaWeiWeatherPage2> with TickerProviderStateMixin{

  double percentage = 0.0;
  @override
  void onCreate() {
    // TODO: implement onCreate
    super.onCreate();
    _animationController = AnimationController(vsync: this, duration: Duration(seconds: 6))
      ..addListener(() {
        setState(() {
          progress = _animationController.value * 360;
        });
      })
      ..addStatusListener((AnimationStatus status) {
        ///动画结束后启动动画
        if (status == AnimationStatus.completed) {
          _animationController.reset();
          _animationController.forward();
        }
      });

    _animationController.forward();
    showToast('===========');
  }

  @override
  Widget buildView(BuildContext context) {
    // TODO: implement buildWidget
    return Container(
      color: Colors.blue,
      child: Column(
        children: <Widget>[
          Text('====='),
          _FanWidget(
            width: 100,
            height: 100,
            progress: progress,
            color: Colors.red,
          ),
        ],
      ),
    );
  }
  double progress = 0;
  AnimationController _animationController;

  @override
  void onDestroy() {
    // TODO: implement onDestroy
//    super.onDestroy();
    globalPrint('========');
    _animationController?.dispose();
  }

  @override
  PreferredSizeWidget initAppbar() {
    // TODO: implement initAppbar
    return null;
  }
}


class BackGrid extends StatelessWidget{
  double percentage;

  BackGrid(this.percentage);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        Container(
          child: Center(
            child: CustomPaint(
              size: Size(300, 300),
              painter: new MyPaint(percent: percentage),
            ),
          ),
        ),

      ],
    );
  }


}

class MyPaint extends CustomPainter{
  Color arcColor;
  double percent = 1;
  double widthR = 15;
  MyPaint({this.arcColor, this.percent = 1});

  @override
  void paint(Canvas canvas, Size size) {
//    double eWidth = size.width / 30;
//    double eHeight = size.height / 30;
    /*画棋盘背景*/
    var paint = Paint()
      ..isAntiAlias = true
      ..style = PaintingStyle.stroke
      ..strokeMiterLimit = 5
      ..strokeWidth = widthR
      ..strokeCap = StrokeCap.round//圆角画笔
      ..color = Color(0xffa5b0c7);/*背景颜色*/
//    canvas.drawRect(Offset.zero & size, paint);
//    /*画棋盘网格*/
//    paint
//      ..style = PaintingStyle.stroke
//      ..color = Colors.red
//      ..strokeWidth = 1.0;
//    for(int i=0;i<=30;i++){
//      double dy = eHeight * i;
//      canvas.drawLine(Offset(0, dy), Offset(size.width, dy), paint);
//    }
//    for(int i=0;i<=30;i++){
//      double dx = eWidth * i ;
//      canvas.drawLine(Offset(dx, 0), Offset(dx, size.height), paint);
//    }
    double centerW = size.width / 2;
    double centerH = size.height / 2;
    Rect rect = Rect.fromCenter(center: Offset(centerW, centerH),width: size.width,height: size.height);
    double startAngle = pi / 3 * 2;
    double sweepAngle = pi / 3 * 5  ;
    canvas.drawArc(rect, startAngle, sweepAngle, false, paint);
    paint.color = Colors.white;
    canvas.drawArc(rect, startAngle, sweepAngle * percent, false, paint);
//    canvas.drawPoints(PointMode.points, [
//      Offset(100, 100)
//    ], paint);
//    canvas.drawCircle(Offset((R + (R - r) * sin(PI / 12)), (R  + (R - r) * cos(PI / 12)) ), r, paint);
//    canvas.drawCircle(Offset((R / 2 - (R - r) * sin(PI / 12)), (R / 2 + (R - r) * cos(PI / 12))), r, paint);
//    canvas.drawCircle(Offset(centerW, centerH), 10, paint);
  }

  @override
  bool shouldRepaint(MyPaint oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}


class _FanWidget extends StatelessWidget {
  final double width;
  final double height;
  final double progress;
  final Color color;

  _FanWidget(
      {this.width = 100, this.height = 100, this.progress = 0, this.color});

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _CustomPainter(width, height, this.progress, this.color),
      size: Size(width, height),
    );
  }
}

class _CustomPainter extends CustomPainter {
  final double width;
  final double height;
  final Color color;

  ///旋转进度
  final double progress;

  ///半径
  double r;

  ///1角度 =  radians弧度
  double radians = pi / 180;

  _CustomPainter(this.width, this.height, this.progress, this.color);

  @override
  void paint(Canvas canvas, Size size) async {
    r = width / 2;
    var fanPath = Path();

    var paint = Paint()
      ..strokeWidth = 1
      ..style = PaintingStyle.fill
      ..color = color;

    ///扇叶的宽度
    double fanWidth = height / 3;

    ///圆柱
    var pillarPath = Path();
    pillarPath.moveTo(r + 1, fanWidth + 3);
    pillarPath.lineTo(r + 4, height - 2);
    pillarPath.quadraticBezierTo(r, height, r - 4, height - 2);
    pillarPath.lineTo(r - 1, fanWidth + 3);
    pillarPath.lineTo(r + 1, fanWidth + 3);
    pillarPath.close();
    canvas.drawPath(pillarPath, paint);

    ///中间圆点
    canvas.drawCircle(Offset(r, fanWidth), 2, paint);

    ///初始的扇叶。
    ///初始时旋转的原点在(0,0)，平移原点到圆心
    canvas.translate(r, fanWidth);

    ///旋转
    canvas.rotate(radians * progress);

    ///留2个宽度放圆点
    fanPath.moveTo(2, 0);
    fanPath.quadraticBezierTo(fanWidth / 4, -4, fanWidth / 2, -2);
    fanPath.lineTo(fanWidth, 0);
    fanPath.lineTo(fanWidth / 2, 2);
    fanPath.quadraticBezierTo(fanWidth / 4, 4, 2, 0);
    fanPath.close();
    canvas.drawPath(fanPath, paint);

    ///第二个扇叶
    canvas.save();
    canvas.rotate(radians * 120);
    canvas.drawPath(fanPath, paint);
    canvas.restore();

    ///第三个扇叶
    canvas.save();
    canvas.rotate(radians * 240);
    canvas.drawPath(fanPath, paint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

class _PageState extends State<FanWidget> with SingleTickerProviderStateMixin {
  double progress = 0;
  AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController =
    AnimationController(vsync: this, duration: Duration(seconds: 6))
      ..addListener(() {
        setState(() {
          progress = _animationController.value * 360;
        });
      })
      ..addStatusListener((AnimationStatus status) {
        if (status == AnimationStatus.completed) {
          _animationController.reset();
          _animationController.forward();
        }
      });

    _animationController.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _FanWidget(
        width: widget.width,
        height: widget.height,
        progress: progress,
        color: widget.color,
      ),
    );
  }

  @override
  void dispose() {
    _animationController?.dispose();
    super.dispose();
  }
}

///风车widget
class FanWidget extends StatefulWidget {
  final double width;
  final double height;
  final Color color;

  FanWidget({this.width, this.height, this.color = Colors.white});

  @override
  State<StatefulWidget> createState() {
    return _PageState();
  }
}